package com.example.common_library.view.keyboard;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.FrameLayout;

/**
 * @desc
 */
public class KeyboardLayout extends FrameLayout {

    private KeyboardLayoutListener mListener;
    private boolean mIsKeyboardActive = false; //输入法是否激活

    private int mKeyboardHeight = 0; // 输入法高度

    public KeyboardLayout(Context context) {
        this(context, null, 0);

    }


    public KeyboardLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public KeyboardLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr); // 监听布局变化

        getViewTreeObserver().addOnGlobalLayoutListener(new KeyboardOnGlobalChangeListener());

    }

    public void setKeyboardListener(KeyboardLayoutListener listener) {

        mListener = listener;

    }

    public KeyboardLayoutListener getKeyboardListener() {
        return mListener;

    }

    public boolean isKeyboardActive() {
        return mIsKeyboardActive;

    }

    /**
     * 获取输入法高度
     *
     * @return
     */

    public int getKeyboardHeight() {
        return mKeyboardHeight;

    }

    public interface KeyboardLayoutListener {

        /**
         * @param isActive       输入法是否激活
         * @param keyboardHeight 输入法面板高度
         */

        void onKeyboardStateChanged(boolean isActive, int keyboardHeight);

    }

    private class KeyboardOnGlobalChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {

        int mScreenHeight = 0;

        private int getScreenHeight() {
            if (mScreenHeight > 0) {
                return mScreenHeight;

            }

            mScreenHeight = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))

                    .getDefaultDisplay().getHeight();
            return mScreenHeight;

        }

        @Override

        public void onGlobalLayout() {

            Rect rect = new Rect();  // 获取当前页面窗口的显示范围

            ((Activity) getContext()).getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
            int screenHeight = getScreenHeight();
            int keyboardHeight = screenHeight - rect.bottom; // 输入法的高度

            boolean isActive = false;
            if (Math.abs(keyboardHeight) > screenHeight / 4) {

                isActive = true; // 超过屏幕五分之一则表示弹出了输入法

                mKeyboardHeight = keyboardHeight;

            }
            Log.e("KeyboardLayout", "isActive: " + isActive + "...keyboardHeight: " + keyboardHeight);
            mIsKeyboardActive = isActive;
            if (mListener != null) {

                mListener.onKeyboardStateChanged(isActive, keyboardHeight);

            }

        }

    }
}
